package com.solace.selenium.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.solace.selenium.entity.SysTask;
import com.solace.selenium.entity.VideoConf;
import com.solace.selenium.entity.VideoGroup;
import com.solace.selenium.entity.VideoOps;
import com.solace.selenium.enums.ConfTypeEnum;
import com.solace.selenium.mapper.VideoConfMapper;
import com.solace.selenium.service.TaskService;
import com.solace.selenium.service.VideoConfService;
import com.solace.selenium.service.VideoGroupService;
import com.solace.selenium.service.VideoOpsService;
import com.solace.selenium.utils.LocalDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
@Transactional
public class VideoConfServiceImpl extends ServiceImpl<VideoConfMapper, VideoConf> implements VideoConfService {
    @Autowired
    private VideoOpsService videoOpsService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private VideoGroupService videoGroupService;
    @Override
    public void startVideoConf(LocalDateTime time) {
        List<VideoConf> confList = findAll();
        Assert.notNull(time,"开始时间不能为空");
        LocalDateTime now = LocalDateTime.now();
        boolean after = time.isAfter(now);
        Assert.isTrue(after,"开始时间必须大于当前时间!");
        boolean empty = CollectionUtils.isEmpty(confList);
        Assert.isTrue(!empty,"配置项不能为空");
        //将配置转化为定时任务
        List<SysTask> cronConfList = new ArrayList<>();
        /*Collections.sort(confList, new Comparator<VideoConf>() {
            @Override
            public int compare(VideoConf o1, VideoConf o2) {
                if (o1.getSort()>o2.getSort()){
                    return -1;
                }else if (o1.getSort()<o2.getSort()){
                    return 1;
                }
                return 0;
            }
        });*/

        for (VideoConf videoConf : confList) {
            Integer cycle = videoConf.getCycle()==null?1:videoConf.getCycle();
            for (int i = 0; i <cycle; i++) {
                Integer type = videoConf.getType();
                String opsData = videoConf.getOpsData();
                LocalDateTime maxTime = time;
                if (ConfTypeEnum.GROUP.getType().equals(type)){
                    List<VideoOps> groupId = videoOpsService.findByGroupId(opsData);
                    if (!CollectionUtils.isEmpty(groupId)){
                        for (VideoOps videoOps : groupId) {
                            LocalDateTime localDateOpsTime = time;
                            Integer hours = videoOps.getHours();
                            Integer minutes = videoOps.getMinutes();
                            Integer seconds = videoOps.getSeconds();
                            if (hours!=null){
                                localDateOpsTime = localDateOpsTime.plusHours(hours);
                            }
                            if (minutes!=null){
                                localDateOpsTime = localDateOpsTime.plusMinutes(minutes);
                            }
                            if (seconds!=null){
                                localDateOpsTime = localDateOpsTime.plusSeconds(seconds);
                            }
                            maxTime = localDateOpsTime.isAfter(time)?localDateOpsTime:maxTime;
                            if ("间隔".equals(videoOps.getOps())) {
                                continue;
                            }
                            SysTask task = new SysTask();
                            String ops = videoOps.getOps();
                            String goodsId = videoOps.getGoodsId();
                            String goodsNumber = videoOps.getGoodsNumber();
                            task.setOps(ops);
                            task.setGoodId(goodsId);
                            task.setNumber(goodsNumber);
                            String cron = LocalDateUtils.localDateToCron(localDateOpsTime);
                            task.setCron(cron);
                            cronConfList.add(task);
                        }
                        time = maxTime;
                    }
                }else if (ConfTypeEnum.INTERVAL.getType().equals(type)){
                    //添加间隔时间
                    Assert.hasText(opsData,"请输入间隔时长(秒)");
                    time = time.plusSeconds(Long.valueOf(opsData));
                }
            }


        }
        taskService.addAndStart(cronConfList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addVideoConf(List<VideoConf> confList) {
        boolean empty = CollectionUtils.isEmpty(confList);
        Assert.isTrue(!empty,"配置项不能为空");
        //删除配置
        delAll();
        //保存
        for (VideoConf videoConf : confList) {
            Integer cycle = videoConf.getCycle();
            if (cycle==null){
                videoConf.setCycle(1);
            }
        }
        saveBatch(confList);
    }

    @Override
    public List<VideoConf> findAll() {
        LambdaQueryWrapper<VideoConf> wrapper = new QueryWrapper<VideoConf>()
                .lambda()
                .orderByAsc(VideoConf::getSort);
        List<VideoConf> list = list(wrapper);
        if (!CollectionUtils.isEmpty(list)){
            for (VideoConf videoConf : list) {
                Integer type = videoConf.getType();
                if (ConfTypeEnum.GROUP.getType().equals(type)){
                    String opsData = videoConf.getOpsData();
                    VideoGroup group = videoGroupService.getById(opsData);
                    if (group!=null){
                        videoConf.setGroupName(group.getName());
                    }
                }
            }
        }
        return list;
    }

    @Override
    public void delByGroup(String groupId) {
        LambdaQueryWrapper<VideoConf> wrapper = new QueryWrapper<VideoConf>()
                .lambda()
                .eq(VideoConf::getOpsData,groupId);
        remove(wrapper);
    }

    @Override
    public void delAll() {
        LambdaQueryWrapper<VideoConf> wrapper = new QueryWrapper<VideoConf>()
                .lambda();
        remove(wrapper);
    }
}